package com.springcloud.security;

import com.springcloud.commons.utils.DigestUtils;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.io.UnsupportedEncodingException;

/**
 * @program: springcloud
 * @description: 自定义的密码处理逻辑，采用md5加密
 * @author: paddy
 * @create: 2019-04-15 12:46
 **/
public class PasswordEncoderImpl implements PasswordEncoder {

    @Override
    public String encode(CharSequence charSequence) {
        try {
            // MD5加密密码
            return DigestUtils.md5Hex(charSequence.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean matches(CharSequence charSequence, String s) {
        return encode(charSequence).equals(s);
    }
}

